// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router'

const routes = [
    {
        path: '/',
        name: 'home',
        component: () => import('@/views/HomeView.vue')
    },
    {
        path: '/search',
        name: 'search',
        component: () => import('@/views/SearchView.vue'),
        props: route => ({ query: route.query.q }) // 将查询参数作为prop传递
    },
    {
        path: '/category/:id',
        name: 'category',
        component: () => import('@/components/TCategoryNav.vue'),
        props: true // 启用路由参数作为props传递
    },
    {
        path: '/product/:id',
        name: 'product',
        component: () => import('@/views/ProductView.vue'),
        props: true
    },
    {
        path: '/cart',
        name: 'cart',
        component: () => import('@/views/CartView.vue')
    },
    // 404页面处理
    {
        path: '/:pathMatch(.*)*',
        redirect: '/'
    },
    {
        path: '/chat',
        name: 'chat',
        component: () => import('@/views/chat/KeFu.vue')
    },
    {
        path: '/kefu',
        name: 'kefu',
        component: () => import('@/views/chat/Home.vue'),
        props: true
    },
]

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes,
    scrollBehavior(to, from, savedPosition) {
        // 商品详情页返回时保持滚动位置
        if (savedPosition) return savedPosition
        // 其他情况滚动到顶部
        return { top: 0 }
    }
})

export default router